package com.shop.shoporder.service.impl;


import com.shop.shoporder.entity.GoodSpec;
import com.shop.shoporder.entity.GoodsDetailsSpce;
import com.shop.shoporder.entity.GroupbuyGoodDetail;
import com.shop.shoporder.entity.WapGoodsSpec;
import com.shop.shoporder.mapper.WapGoodsDetailsMapper;
import com.shop.shoporder.service.WapGoodsDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
@Service("wapGoodsDetailsService")
public class WapGoodsDetailsServiceImpl implements WapGoodsDetailsService {

    @Autowired
    WapGoodsDetailsMapper wapGoodsDetailsMapper;

    @Autowired
    RedisTemplate redisTemplate;
    /**
     * 根据商品ID查询商品详情
     * @param map
     * @return
     * @throws Exception
     */
    @Override
    public WapGoodsSpec selectGoodsDetailsByGoodId(Map<String, String> map) throws Exception {
        if(map!=null&&map.get("Id")!=null &&map.get("specId")!=null){
            int Id=0;
            Id=Integer.parseInt(map.get("Id"));

            int specId=0;
            specId=Integer.parseInt(map.get("specId"));
            WapGoodsSpec wapGoodsSpec=null;

            Object obj=redisTemplate.opsForValue().get("goodsDetail"+Id+specId);
            if(obj!=null){
                wapGoodsSpec=(WapGoodsSpec) obj;
            }else{
                wapGoodsSpec=wapGoodsDetailsMapper.selectGoodsDetailsByGoodIdAndSpecId(Id,specId);
            }


            if(wapGoodsSpec!=null){
                return wapGoodsSpec;
            }else{
                return null;
            }

        }else{
            return null;
        }
    }

    @Override
    public List<GoodsDetailsSpce> selectDetailsSpecListByGoodsId(Map<String, String> map) {
        if(map!=null&&map.get("Id")!=null){
            int Id=0;
            Id=Integer.parseInt(map.get("Id"));
            //获取缓存的数据
            List<GoodsDetailsSpce> goodsDetailsSpceList=null;
           Object object= redisTemplate.opsForValue().get("specs"+Id);
            if(object!=null){
                goodsDetailsSpceList=(List<GoodsDetailsSpce>)object;
            }else{
                goodsDetailsSpceList=wapGoodsDetailsMapper.selectDetailsSpecListByGoodsId(Id);
            }

            if(goodsDetailsSpceList!=null&&goodsDetailsSpceList.size()!=0){
                return goodsDetailsSpceList;
            }else{
                return null;
            }
        }else{
            return null;
        }


    }

    @Override
    public GoodSpec getGoodSpecDetail(int goodsId, int specId) {
        return wapGoodsDetailsMapper.getGoodSpecDetail(goodsId,specId);
    }

    @Override
    public GroupbuyGoodDetail getGroupbuyGoodDetail(int goodsId) {
        return wapGoodsDetailsMapper.getGroupbuyGoodDetail(goodsId);
    }


}
